import '/@/design/index.less';
import 'virtual:windi-base.css';
import 'virtual:windi-components.css';
import 'virtual:windi-utilities.css';
// Register icon sprite
import 'virtual:svg-icons-register';
import App from './App.vue';
import { createApp } from 'vue';
import { initAppConfigStore } from '/@/logics/initAppConfig';
import { setupErrorHandle } from '/@/logics/error-handle';
import { router, setupRouter } from '/@/router';
import { setupRouterGuard } from '/@/router/guard';
import { setupStore } from '/@/shared/store';
import { setupGlobDirectives } from '/@/directives';
import { setupI18n } from '/@/locales/setupI18n';
import { registerGlobComp } from '/@/components/registerGlobComp';
import { AppPreBootstrap } from './AppPreBootstrap';
// Importing on demand in local development will increase the number of browser requests by around 20%.
// This may slow down the browser refresh speed.
// Therefore, only enable on-demand importing in production environments .
if (import.meta.env.DEV) {
  import('ant-design-vue/dist/antd.less');
}
import { appSessionService } from './shared/abp';
import { setupAntd } from './shared/antd/import-antd';
import { ModalHelper, SignalRAspNetCoreHelper, UserNotificationHelper } from './shared/helpers';
import PageHeaderComponents from '/@/components/page-header/page-header.vue';
// Vue.config.productionTip = false;
import { setupComp } from './global-component-install';
import { refreshTokenService } from './shared/auth/refresh-token';
import { abpSignalrService } from './shared';

async function bootstrap() {
  const app = createApp(App);
  // 注册表头
  app.component('PageHeader', PageHeaderComponents);
  // 通知服务
  if (appSessionService.userId) {
    SignalRAspNetCoreHelper.initSignalR(() => {
      abp.event.on('abp.notifications.received', (userNotification) => {
        UserNotificationHelper.show(userNotification);
      });
      // 聊天服务
      abpSignalrService.init();
    });
    // 注册刷新token定时器
    refreshTokenService.initTimer();
  }

  // 模态窗注册
  ModalHelper.init(app);
  // 按需注册antd
  setupAntd(app);

  // 注册全局组件
  setupComp(app);

  // Configure store
  setupStore(app);
  // Initialize internal system configuration
  initAppConfigStore();

  // Register global components
  registerGlobComp(app);

  // Multilingual configuration
  // Asynchronous case: language files may be obtained from the server side
  await setupI18n(app);

  // Configure routing
  setupRouter(app);

  // router-guard
  setupRouterGuard(router);

  // Register global directive
  setupGlobDirectives(app);

  // Configure global error handling
  setupErrorHandle(app);

  // https://next.router.vuejs.org/api/#isready
  // await router.isReady();
  // setupAntd(app)
  app.mount('#app');
}

// bootstrap();
AppPreBootstrap.run(() => {
  //   // 初始化会话信息

  appSessionService.init().then(() => {
    bootstrap().then(() => {});
  });
});
